package com.xingda.packing.dmds.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.dmds.enums.WorkAreaType;
import com.xingda.packing.dmds.events.dto.EventTriggerAlarmSaveDto;
import com.xingda.packing.dmds.minitor.dto.PkAlarmDto;
import com.xingda.packing.dmds.minitor.entity.PkAlarmHistory;
import com.xingda.packing.dmds.minitor.entity.PkAlarmType;
import com.xingda.packing.dmds.minitor.mapper.PkAlarmHistoryMapper;
import com.xingda.packing.dmds.minitor.mapper.PkAlarmTypeMapper;
import com.xingda.packing.dmds.minitor.service.IPkAlarmHistoryService;
import com.xingda.packing.dmds.rabbitmq.DTSAlarmDirectPublisher;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service("pkAlarmHistoryDMDSService")
public class PkAlarmHistoryServiceImpl extends ServiceImpl<PkAlarmHistoryMapper, PkAlarmHistory> implements IPkAlarmHistoryService {
    @Resource
    PkAlarmTypeMapper pkAlarmTypeMapper;

    @Resource
    DTSAlarmDirectPublisher dtsAlarmDirectPublisher;

    @Override
    public List<PkAlarmDto> getList(int area) {

        List<PkAlarmType> pkAlarmTypeList = pkAlarmTypeMapper.selectList(Wrappers.lambdaQuery());
        if (CollectionUtils.isEmpty(pkAlarmTypeList)) {
            return null;
        }
        LambdaQueryWrapper<PkAlarmHistory> pkAlarmHistorQueryWrapper = Wrappers.lambdaQuery();
        pkAlarmHistorQueryWrapper.isNull(PkAlarmHistory::getEndDateTime).orderByDesc(PkAlarmHistory::getStartDateTime);
        pkAlarmHistorQueryWrapper.eq(PkAlarmHistory::getArea, area);
        List<PkAlarmHistory> pkAlarmHistoryList = this.list(pkAlarmHistorQueryWrapper);
        if (CollectionUtils.isEmpty(pkAlarmHistoryList)) {
            return null;
        }
        List<PkAlarmDto> list = new ArrayList<>();
        for (PkAlarmHistory pkAlarmHistory : pkAlarmHistoryList) {
            Optional<PkAlarmType> opt = pkAlarmTypeList.stream().filter(p -> p.getId() == pkAlarmHistory.getAlarmTypeId()).findFirst();
            if (opt.isPresent()) {
                PkAlarmDto pkAlarmDto = new PkAlarmDto();
                pkAlarmDto.setContent(opt.get().getContent());
                pkAlarmDto.setCreateTime(pkAlarmHistory.getStartDateTime());
                list.add(pkAlarmDto);
            }
        }
        return list;
    }

    @Override
    public void saveTriggerAlarm(EventTriggerAlarmSaveDto dto) {
        PkAlarmType pkAlarmType = dto.getPkAlarmType();
        if (Objects.isNull(dto.getPkAlarmType())) {
            LambdaQueryWrapper<PkAlarmType> pkAlarmTypeQueryWrapper = Wrappers.lambdaQuery();
            pkAlarmTypeQueryWrapper.eq(PkAlarmType::getAlarmAreaId, dto.getAlarmTriggerType().getAreaId()).eq(PkAlarmType::getAddressIndex, dto.getAlarmTriggerType().getAddressIndex());
            pkAlarmType = pkAlarmTypeMapper.selectOne(pkAlarmTypeQueryWrapper);
        }
        LambdaQueryWrapper<PkAlarmHistory> pkAlarmHistoryQueryWrapper = Wrappers.lambdaQuery();
        pkAlarmHistoryQueryWrapper.eq(PkAlarmHistory::getAlarmTypeId, pkAlarmType.getId()).eq(PkAlarmHistory::getArea, dto.getArea()).isNull(PkAlarmHistory::getEndDateTime);
        List<PkAlarmHistory> pkAlarmHistorieList = baseMapper.selectList(pkAlarmHistoryQueryWrapper);
        // 触发报警
        if (dto.isTrigger()) {
            if (CollectionUtils.isEmpty(pkAlarmHistorieList)) {
                PkAlarmHistory pkAlarmHistory = new PkAlarmHistory();
                pkAlarmHistory.setAlarmTypeId(pkAlarmType.getId());
                pkAlarmHistory.setStartDateTime(new Date());
                pkAlarmHistory.setArea(dto.getArea());
                //如果这个报警需要发送，而又没有消除报警的触发，则直接设置结束时间，如轮子99,999,9999
                if (pkAlarmType.isSend() && !pkAlarmType.isRelieveNotice()) {
                    pkAlarmHistory.setEndDateTime(new Date());
                }
                if (pkAlarmType.isSend()) {
                    dtsAlarmDirectPublisher.sendAllWorkerAlarm(pkAlarmType, "区域:" + WorkAreaType.getByID(dto.getArea()).getTypeName(), dto.getArea());
                    pkAlarmHistory.setSendState(2);
                }
                save(pkAlarmHistory);
            }
        } else {
            // 消除报警
            if (!CollectionUtils.isEmpty(pkAlarmHistorieList)) {
                PkAlarmHistory pkAlarmHistory = pkAlarmHistorieList.get(0);
                pkAlarmHistory.setEndDateTime(new Date());
                updateById(pkAlarmHistory);
                if (pkAlarmType.isSend()) {
                    dtsAlarmDirectPublisher.sendAllWorkerNotice(pkAlarmType, "区域:" + WorkAreaType.getByID(dto.getArea()).getTypeName(), dto.getArea());
                }
            }
        }
    }
}
